/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-1-10 下午12:04 Created by lyyang
 */
package com.jade.bss.business.groupon.impl;

import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.jade.bss.business.groupon.SnifferSetting;
import com.jade.bss.business.groupon.SnifferSettingDao;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;

/**
 * 团购券嗅探器设置dao的jpa实现
 *
 * @author <a href="mailto:lyyang@ceno.cn">lyyang</a>
 * @version 1.0 15-1-10 下午12:04
 */
@Named ("ags_snifferSettingDao")
public class JpaSnifferSettingDao implements SnifferSettingDao
{
    @PersistenceContext (unitName = "default")
    protected EntityManager entityManager;

    public void add(SnifferSetting setting){
        entityManager.persist(setting);
    }

    public  void update(SnifferSetting setting){
        entityManager.merge(setting);
    }

    public void remove(long... ids){

        String sql = "DELETE FROM SnifferSetting WHERE id in (:ids)";
        Query query = entityManager.createQuery(sql);
        List<Long> list = new ArrayList<Long>();
        for (long id:ids){
            list.add(id);
        }
        query.setParameter("ids",list);
        query.executeUpdate();
    }

    public void remove(String ownerId, String ownerType){
        String sql = "DELETE FROM SnifferSetting WHERE ownerId=:ownerId AND ownerType=:ownerType";
        Query query = entityManager.createQuery(sql);
        query.setParameter("ownerId",ownerId);
        query.setParameter("ownerType",ownerType);
        query.executeUpdate();
    }

    public SnifferSetting get(long id){
        return entityManager.find(SnifferSetting.class,id);
    }

    public List<SnifferSetting> list(String ownerId, String ownerType){
       String sql = "SELECT entity FROM SnifferSetting entity WHERE ownerId=:ownerId AND ownerType=:ownerType";
        Query query = entityManager.createQuery(sql);
        query.setParameter("ownerId",ownerId);
        query.setParameter("ownerType",ownerType);
        return query.getResultList();
    }


    public SnifferSetting get(String ownerId, String ownerType, String channel){
       String sql = "SELECT entity FROM SnifferSetting entity WHERE ownerId=:ownerId AND ownerType=:ownerType AND channel=:channel";
        Query query = entityManager.createQuery(sql);
        query.setParameter("ownerId",ownerId);
        query.setParameter("ownerType",ownerType);
        query.setParameter("channel",channel);
        try{
            return (SnifferSetting)query.getSingleResult();

        }
        catch (NoResultException ex){

        }
        return null;
    }

    public List<SnifferSetting> list(Criteria criteria){
         String sql = "SELECT entity FROM SnifferSetting entity";
        return SqlUtils.sqlQueryWithoutCount(sql, entityManager, criteria);
    }

    public PagingList<SnifferSetting> query(Criteria criteria){
         String sql = "SELECT entity FROM SnifferSetting entity";
        return SqlUtils.sqlQuery(sql, entityManager, criteria);
    }
}
